package com.itheima.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.constant.RedisConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.config.AliOssProperties;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private AliOssProperties aliOssProperties;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    @Transactional
    public void add(Integer[] checkgroupIds, Setmeal setmeal) {
        //添加套餐
        setmealMapper.add(setmeal);
        //添加套餐和检查组关系
        setmealMapper.addGroups(checkgroupIds,setmeal.getId());
        //缓存套餐图片到redis
        SetOperations setOperations = redisTemplate.opsForSet();
        setOperations.add(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
    }

    @Override
    @Transactional
    public void edit(Integer[] checkgroupIds, Setmeal setmeal) {
        setmealMapper.edit(setmeal);
        //删除套餐和检查组关系
        setmealMapper.deleteGroups(setmeal.getId());
        //添加新的套餐和检查组关系
        setmealMapper.addGroups(checkgroupIds,setmeal.getId());

    }

    @Override
    public Setmeal findById(Integer id) {
        return setmealMapper.findById(id);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        PageInfo<Setmeal> pageInfo = new PageInfo<>(setmealMapper.findPage(queryPageBean.getQueryString()));
        return new  PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public String upload(MultipartFile imgFile) {
        String fileName = imgFile.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        String newFileName = System.currentTimeMillis() + suffix;

        AliOssUtil aliOssUtil = new AliOssUtil(aliOssProperties.getEndpoint(),  aliOssProperties.getAccessKeyId(),
                aliOssProperties.getAccessKeySecret(), aliOssProperties.getBucketName());
        try {
            String url = aliOssUtil.upload(imgFile.getBytes(), newFileName);
            //存入redis
            SetOperations setOperations = redisTemplate.opsForSet();
            setOperations.add(RedisConstant.SETMEAL_PIC_RESOURCES,url);
            return url;
        } catch (Exception e) {
            return "图片上传失败";
        }
    }
}
